<template>
  <div class="tpl-container">
    <div class="mb20 condition-container">
      <div class="condition">
        <span class="color_494949 ft12 pr5">组合编码：</span>
        <ElInput
          v-model="CSCode"
          class="w164 mr14"
          placeholder="请输入彩屏编码"
        />
      </div>
      <div class="condition">
        <span class="color_494949 ft12 pr5">正反面：</span>
        <!-- <ElInput v-model="CSName" class="w164" placeholder="请输入彩屏名称" /> -->
        <el-select v-model="CSName" class="m-2" placeholder="Select">
          <el-option
            v-for="item in csNames"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </div>
      <div class="condition">
        <span class="color_494949 ft12 pr5">屏幕类型：</span>
        <el-select v-model="LCDType" class="m-2" placeholder="Select">
          <el-option
            v-for="item in types"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </div>
      <div class="condition">
        <span class="color_494949 ft12 pr5">商品组合：</span>
        <ElInput v-model="MPCode" class="w164" placeholder="请输入商品组合" />
      </div>
      <div class="condition">
        <span class="color_494949 ft12 pr5">彩屏模板：</span>
        <ElInput v-model="CSTypeID" class="w164" placeholder="请输入彩屏名称" />
      </div>
      <div class="condition">
        <ElButton class="settingBtn" type="primary" @click="search">
          查询
        </ElButton>
        <!-- <ElButton
          v-if="MenuAuth('add')"
          class="settingBtn"
          type="primary"
          @click="addFn"
        >
          新增
        </ElButton> -->
      </div>
    </div>
    <div class="btns">
      <Tabs
        :multiple-selection="multipleSelection"
        @confirm="tabsDialogConfirm"
      />
    </div>
    <div class="table">
      <ElTable
        ref="multipleTableRef"
        v-loading="loading"
        :border="true"
        :data="tableData"
        @selection-change="handleSelectionChange"
      >
        <ElTableColumn type="selection" width="55" />
        <ElTableColumn label="公司" prop="CompanyName" width="auto" />
        <ElTableColumn label="彩屏标识" prop="UUID" width="auto" />
        <ElTableColumn label="内部编码" prop="Code" width="auto" />
        <ElTableColumn label="组合编码" prop="CSCode" width="auto" />
        <ElTableColumn label="正反面" prop="CSName" width="auto" />
        <ElTableColumn label="彩屏模板" prop="CSTypeID" width="auto" />
        <ElTableColumn label="商品组合" width="auto">
          <template #default="{ row }">
            <div v-if="row.tags && row.tags.length && row.tags[0] !== ''">
              <el-tag v-for="tag in row.tags" :key="tag" class="tag">
                <span>{{ tag }}</span>
              </el-tag>
            </div>
          </template>
        </ElTableColumn>
        <!-- <ElTableColumn label="屏幕类型" prop="LCDType" width="auto" /> -->
        <ElTableColumn label="彩屏图片" min-width="80">
          <template #default="{ row }">
            <div class="img-container">
              <ElImage
                fit="contain"
                :hide-on-click-modal="true"
                :initial-index="9"
                :preview-src-list="[row.PathImg]"
                :src="row.PathImg"
                :z-index="99999"
                :zoom-rate="1.2"
              />
            </div>
          </template>
        </ElTableColumn>
        <ElTableColumn label="屏幕类型">
          <template #default="{ row }">
            <div>
              {{
                row.LCDType == '00'
                  ? '600*1024'
                  : row.LCDType == '03'
                  ? '800*1280'
                  : '默认类型'
              }}
            </div>
          </template>
        </ElTableColumn>
        <ElTableColumn label="显示方式">
          <template #default="{ row }">
            <div>
              {{ row.OATUpgrade == 0 ? '横屏' : '竖屏' }}
            </div>
          </template>
        </ElTableColumn>
        <!-- <ElTableColumn label="WIFI名称" prop="WFSSID" width="auto" /> -->
        <!-- <ElTableColumn label="音量" prop="Volume" width="auto" /> -->
        <!-- <ElTableColumn label="内部码" prop="Code" width="auto" /> -->
        <ElTableColumn label="显示模式">
          <template #default="{ row }">
            <div>
              {{
                row.ChangeTemp == '1'
                  ? '全屏图片'
                  : row.ChangeTemp == '2'
                  ? '视频图片'
                  : row.ChangeTemp == '3'
                  ? '图片轮播'
                  : row.ChangeTemp == '4'
                  ? '全屏轮播'
                  : row.ChangeTemp == '5'
                  ? '全屏视频'
                  : ''
              }}
            </div>
          </template>
        </ElTableColumn>
        <ElTableColumn label="推送状态">
          <template #default="{ row }">
            <div>
              {{ row.SendStatus == 1 ? '正常' : '未生效' }}
            </div>
          </template>
        </ElTableColumn>
        <ElTableColumn label="程序升级">
          <template #default="{ row }">
            <div>
              {{ row.AppUpgradeStatus == 0 ? '待升级' : '已发送升级' }}
            </div>
          </template>
        </ElTableColumn>
        <!-- <ElTableColumn label="固件升级" prop="ESLWidth" width="auto" /> -->
        <ElTableColumn label="固件升级">
          <template #default="{ row }">
            <div>
              {{ row.OATUpgradeStatus == 0 ? '待升级' : '已发送升级' }}
            </div>
          </template>
        </ElTableColumn>
        <ElTableColumn label="在线状态">
          <template #default="{ row }">
            <div>
              {{ row.onlineStatus == 1 ? '正常' : '离线' }}
            </div>
          </template>
        </ElTableColumn>
        <ElTableColumn label="操作">
          <template #default="{ row }">
            <ElButton
              v-if="MenuAuth('edit')"
              link
              type="primary"
              @click="editFn(row)"
            >
              修改
            </ElButton>
          </template>
        </ElTableColumn>
      </ElTable>
      <div v-if="total > 0" class="pagination">
        <ElPagination
          background
          :current-page="searchInfo.pageNo"
          :layout="layout"
          :page-size="searchInfo.pageSize"
          :total="total"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </div>
    </div>
  </div>
  <editDialog
    :data="csData"
    title="编辑"
    :visible="editDialogVisible"
    @close="editHandleClose"
    @confirm="editHandleConfirm"
  />
</template>

<script lang="ts" setup>
  import { ref } from 'vue'
  import { MenuAuth } from '~/src/utils/permission'
  import { getPageData, csedit } from '@/api/mupro'
  import Tabs from './components/tabs.vue'
  import { save } from './components/save'
  import editDialog from './components/editDialog.vue'
  const CSCode = ref('')
  const CSName = ref('')
  const MPCode = ref('')
  const CSTypeID = ref('')
  const searchInfo = reactive({
    pageNo: 1,
    pageSize: 20,
    tableName: '',
    sort: '',
    order: '',
    wheres: `[{"name":"CSCode","value":${CSCode.value},"displayType":"like"}, {"name":"CSName","value":${CSName.value},"displayType":"like"}]`,
    export: false,
    value: '',
  })
  const LCDType: any = ref('')
  const types: any = ref([
    {
      label: '全部类型',
      value: '',
    },
    {
      label: '600*1024',
      value: '00',
    },
    {
      label: '800*1280',
      value: '03',
    },
  ])
  const csNames = [
    {
      label: '全部',
      value: '',
    },
    {
      label: '正面',
      value: '正面',
    },
    {
      label: '反面',
      value: '反面',
    },
  ]
  const loading = ref(false)

  const layout = ref('total, sizes, prev, pager, next, jumper')
  const tableData = ref([])
  const total = ref(0)
  // 获取表格数据
  const gettableData = async () => {
    loading.value = true
    const { data } = await getPageData(searchInfo)
    loading.value = false
    tableData.value = data?.rows || []
    const nowTime = new Date().getTime()
    tableData.value.map((item: any) => {
      item.onlineStatus =
        nowTime - new Date(item.ModifyDate).getTime() <= 60 * 1000
      item.tags = item.Remark.split(',')
    })
    total.value = data?.total || 0
  }
  gettableData()
  //分页条数切换
  const handleSizeChange = (val: any) => {
    searchInfo.pageSize = val
    gettableData()
  }
  //分页切换
  const handleCurrentChange = (val: any) => {
    searchInfo.pageNo = val
    gettableData()
  }

  const search = () => {
    searchInfo.pageNo = 1
    if (
      CSCode.value.trim() !== '' ||
      CSName.value.trim() !== '' ||
      LCDType.value.trim() !== '' ||
      MPCode.value.trim() !== '' ||
      CSTypeID.value.trim() !== ''
    ) {
      const arr = []
      CSCode.value.trim() !== '' &&
        arr.push({ name: 'CSCode', value: CSCode.value, displayType: 'like' })
      CSName.value.trim() !== '' &&
        arr.push({ name: 'ESLName', value: CSName.value, displayType: 'like' })
      LCDType.value.trim() !== '' &&
        arr.push({ name: 'LCDType', value: LCDType.value, displayType: 'like' })
      MPCode.value.trim() !== '' &&
        arr.push({ name: 'MPCode', value: MPCode.value, displayType: 'like' })
      CSTypeID.value.trim() !== '' &&
        arr.push({
          name: 'CSTypeID',
          value: CSTypeID.value,
          displayType: 'like',
        })
      searchInfo.wheres = JSON.stringify(arr)
    } else {
      searchInfo.wheres = `[{"name":"CSCode","value": '',"displayType":"like"}, {"name":"CSName","value": '',"displayType":"like"}, {"name":"LCDType","value": '',"displayType":"like"}, {"name":"MPCode","value": '',"displayType":"like"}, {"name":"CSTypeID","value": '',"displayType":"like"}]`
    }
    gettableData()
  }
  const multipleSelection = ref<any[]>([])
  const handleSelectionChange = (val: any[]) => {
    multipleSelection.value = val
  }
  const tabsDialogConfirm = async (
    result: Array<any>,
    type: string,
    multipleSelection: Array<any>
  ) => {
    // console.log(result, type, multipleSelection)
    loading.value = true
    const { data, message } = await save(result, type, multipleSelection)
    loading.value = false
    ElMessage({
      message: message,
      type: 'success',
    })
    await gettableData()
  }
  // 编辑
  const csData = ref({})
  const editDialogVisible = ref(false)
  const editFn = (row: any) => {
    csData.value = row
    editDialogVisible.value = true
  }
  const editHandleConfirm = async (result: any) => {
    csData.value = result
    // console.log(csData.value)
    await csedit(csData.value)
    editDialogVisible.value = false
    gettableData()
  }
  const editHandleClose = () => {
    editDialogVisible.value = false
  }
</script>

<style lang="scss" scoped>
  .tpl-container {
    .color_494949 {
      color: #494949;
    }

    .noData {
      text-align: center;
      font-size: 16px;
      color: gray;
    }
    .w164 {
      width: 164px;
    }
    .mb20 {
      margin-bottom: 20px;
    }

    .img-container {
      display: flex;
      // justify-content: center;

      .img {
        width: 100px;
      }
    }

    .condition-container {
      display: flex;
      flex-wrap: wrap;
      align-items: center;

      .condition {
        margin-right: 14px;
        margin-bottom: 10px;
      }
    }

    .btns {
      margin-bottom: 10px;
    }

    .tag {
      margin-top: 4px;
    }
  }
  .el-table {
    & ::v-deep th.el-table__cell,
    ::v-deep td.el-table__cell {
      // 设置position 使得 子元素不与其产生新的层叠关系
      position: static;
    }
  }
</style>
